package me.xiaosiqi.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.xiaosiqi.dao.store.CourseDao;
import me.xiaosiqi.domain.store.Course;
import me.xiaosiqi.factory.MapperFactory;
import me.xiaosiqi.service.store.CourseService;
import me.xiaosiqi.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author xiaosiqi
 * @date 2021/6/3 14:38
 */
public class CourseServiceImpl implements CourseService {

    @Override
    public void save(Course course) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            String s = UUID.randomUUID().toString();
            course.setId(s);
            course.setCreateTime(new Date());
            courseDao.save(course);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(Course course) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            courseDao.delete(course);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void update(Course course) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            courseDao.update(course);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public Course findById(String id) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            return courseDao.findById(id);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Course> findAll() {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            return courseDao.findAll();
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public PageInfo findPage(int page, int size) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            PageHelper.startPage(page,size);
            List<Course> all = courseDao.findAll();
            return new PageInfo(all);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
